package lt.egidijus.aatest.wizards.helpers;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.ColumnLabelProvider;
import org.eclipse.jface.viewers.ColumnPixelData;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

public class ColumnLabelProviderDemonstrate extends ApplicationWindow {

	// ------------------ //
	// instance variables //
	// ------------------ //

	// Model( of MVC) input
	private final Commodity[] input = new Commodity[] {
			new Commodity("monitor", 99.8), new Commodity("key board", 8.4),
			new Commodity("mouth", 4.8) };

	private TableViewer commodityTableViewer;

	// -------------------- //
	// constructor and main //
	// -------------------- //
	public ColumnLabelProviderDemonstrate() {
		super(null);
		
	}

	public static void main(String[] args) {
		Window w = new ColumnLabelProviderDemonstrate();
		w.setBlockOnOpen(true);
		w.open();
		Display.getCurrent().dispose();
	}

	// ----------------------- //
	// contents create methods //
	// ----------------------- //

	@Override
	protected Control createContents(Composite parent) {

		Composite composite = new Composite(parent, SWT.NONE);
		FillLayout fillLayout = new FillLayout();
		composite.setLayout(fillLayout);

		createCommodityTableViewer(composite);
		commodityTableViewer.setInput(input);

		composite.pack();
		parent.pack();
		return parent;
	}

	protected TableViewer createCommodityTableViewer(Composite parent) {
		commodityTableViewer = new TableViewer(parent, SWT.FULL_SELECTION);

		Table commodityTable = (Table) commodityTableViewer.getControl();

		TableLayout tableLayout = new TableLayout();
		tableLayout.addColumnData(new ColumnPixelData(150));
		tableLayout.addColumnData(new ColumnPixelData(100));
		commodityTable.setLayout(tableLayout);

		commodityTableViewer
				.setContentProvider(new IStructuredContentProvider() {
					@Override
					public Object[] getElements(Object inputElement) {
						return (Commodity[]) inputElement;
					}

					@Override
					public void dispose() {

					}

					@Override
					public void inputChanged(Viewer viewer, Object oldInput,
							Object newInput) {

					}
				});

		createNameColumn();
		createPriceColumn();

		commodityTable.setHeaderVisible(true);

		return commodityTableViewer;
	}

	protected void createNameColumn() {
		TableViewerColumn vNameColumn = new TableViewerColumn(
				commodityTableViewer, SWT.LEFT);
		TableColumn nameColumn = vNameColumn.getColumn();
		nameColumn.setText("Commodity Name");

		vNameColumn.setLabelProvider(new ColumnLabelProvider() {
			public String getText(Object element) {
				return ((Commodity) element).name;
			}
		});

		vNameColumn.setEditingSupport(new EditingSupport(commodityTableViewer) {
			@Override
			protected boolean canEdit(Object element) {
				return true;
			}

			@Override
			protected CellEditor getCellEditor(Object element) {
				return new TextCellEditor((Table) commodityTableViewer
						.getControl());
			}

			@Override
			protected Object getValue(Object element) {
				return ((Commodity) element).name;
			}

			@Override
			protected void setValue(Object element, Object value) {
				((Commodity) element).name = (String) value;
				commodityTableViewer.refresh();
			}
		});
	}

	protected void createPriceColumn() {
		TableViewerColumn vPriceColumn = new TableViewerColumn(
				commodityTableViewer, SWT.RIGHT);
		TableColumn priceColumn = vPriceColumn.getColumn();
		priceColumn.setText("Price");

		vPriceColumn.setLabelProvider(new ColumnLabelProvider() {
			@Override
			public String getText(Object element) {
				return "$" + ((Commodity) element).price;
			}
		});

		vPriceColumn
				.setEditingSupport(new EditingSupport(commodityTableViewer) {
					@Override
					protected boolean canEdit(Object element) {
						return true;
					}

					@Override
					protected CellEditor getCellEditor(Object element) {
						TextCellEditor textCellEditor = new TextCellEditor(
								(Table) commodityTableViewer.getControl());

						return textCellEditor;
					}

					@Override
					protected Object getValue(Object element) {
						return String.valueOf(((Commodity) element).price);
					}

					@Override
					protected void setValue(Object element, Object value) {
						try {
							double doubleValue = Double.valueOf((String) value);
							((Commodity) element).price = doubleValue;
							commodityTableViewer.refresh();
						} catch (NumberFormatException e) {
							return;
						}
					}
				});
	}

	// ------------ //
	// Model of MVC //
	// ------------ //
	private class Commodity {
		public String name;
		public double price;

		public Commodity(String name, double price) {
			this.name = name;
			this.price = price;
		}
	}
}
